package com.mingserve.raw.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mingserve.common.core.utils.SpringUtils;
import com.mingserve.common.core.utils.StringUtils;
import com.mingserve.raw.domain.Equipment;
import com.mingserve.raw.domain.EquipmentConfig;
import com.mingserve.raw.domain.EquipmentRepair;
import com.mingserve.raw.domain.EquipmentThreshold;
import com.mingserve.raw.event.RefreshEvent;
import com.mingserve.raw.mapper.EquipmentConfigMapper;
import com.mingserve.raw.mapper.EquipmentMapper;
import com.mingserve.raw.mapper.EquipmentRepairMapper;
import com.mingserve.raw.mapper.EquipmentThresholdMapper;
import com.mingserve.raw.service.IEquipmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备管理Service接口实现
 *
 * @author lvwshuai
 * @date 2024-02-02
 */
@RequiredArgsConstructor
@Service
public class EquipmentServiceImpl implements IEquipmentService
{
    private final EquipmentMapper equipmentMapper;
    private final EquipmentConfigMapper equipmentConfigMapper;
    private final EquipmentRepairMapper equipmentRepairMapper;
    private final EquipmentThresholdMapper equipmentThresholdMapper;

    /**
     * 批量新增或更新设备
     *
     * @param list 设备列表
     * @return 结果
     */
    @Override
    public Boolean batchEquipment(List<Equipment> list) {
        equipmentMapper.delete(new QueryWrapper<>());
        return equipmentMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询设备列表
     *
     */
    @Override
    public List<Equipment> queryList() {
        return equipmentMapper.selectList();
    }

    /**
     * 按编号查询设备
     *
     * @param equipmentCode
     * @return
     */
    @Override
    public Equipment getEquipmentByCode(String equipmentCode) {
        LambdaQueryWrapper<Equipment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Equipment::getEquipmentCode, equipmentCode);
        lqw.last("limit 1");
        return equipmentMapper.selectOne(lqw);
    }

    /**
     * 按编号查询设备配置
     *
     * @param equipmentCode
     * @return
     */
    @Override
    public EquipmentConfig getEquipmentConfigByCode(String equipmentCode) {
        LambdaQueryWrapper<EquipmentConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EquipmentConfig::getEquipmentCode, equipmentCode);
        lqw.last("limit 1");
        return equipmentConfigMapper.selectOne(lqw);
    }

    /**
     * 更新设备采集配置
     *
     * @param equipmentCode
     * @return
     */
    @Override
    public Boolean updateEquipmentConfigByCode(EquipmentConfig equipmentCode) {
        EquipmentConfig find = getEquipmentConfigByCode(equipmentCode.getEquipmentCode());
        JSONObject config;
        if (find == null) {
            find = new EquipmentConfig();
            find.setEquipmentCode(equipmentCode.getEquipmentCode());
            config = new JSONObject();
        } else {
            config = JSON.parseObject(find.getConfig());
        }
        // 解析接收到的 config 字符串
        if (StringUtils.isNotEmpty(equipmentCode.getConfig())) {
            JSONObject newConfig = JSONObject.parseObject(equipmentCode.getConfig());
            String key = newConfig.keySet().iterator().next();
            config.put(key, newConfig.get(key).toString());
            find.setConfig(JSON.toJSONString(config));
        }
        boolean flag = equipmentConfigMapper.insertOrUpdate(find);
        if (flag) {
            // 刷新传感器配置
            RefreshEvent refreshEvent = new RefreshEvent();
            refreshEvent.setRefreshPoint(false);
            refreshEvent.setEquipmentCode(equipmentCode.getEquipmentCode());
            // 推送设备配置缓存刷新
            SpringUtils.publishEvent(refreshEvent);
        }
        return flag;
    }

    /**
     * 批量新增或更新配置信息
     *
     * @param list
     * @return
     */
    @Override
    public Boolean batchEquipmentConfig(List<EquipmentConfig> list) {
        equipmentConfigMapper.delete(new QueryWrapper<>());
        return equipmentConfigMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询设备配置列表
     *
     * @return 结果
     */
    @Override
    public List<EquipmentConfig> queryConfigList() {
        return equipmentConfigMapper.selectList();
    }

    /**
     * 批量新增或更新维修信息
     *
     * @param list
     * @return
     */
    @Override
    public Boolean batchEquipmentRepair(List<EquipmentRepair> list) {
        equipmentRepairMapper.delete(new QueryWrapper<>());
        return equipmentRepairMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询设备维修列表
     *
     * @return 结果
     */
    @Override
    public List<EquipmentRepair> queryRepairList() {
        return equipmentRepairMapper.selectList();
    }

    /**
     * 批量新增或更新设备阈值信息
     *
     * @param list
     * @return
     */
    @Override
    public Boolean batchEquipmentThreshold(List<EquipmentThreshold> list) {
        equipmentThresholdMapper.delete(new QueryWrapper<>());
        return equipmentThresholdMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询设备阈值列表
     *
     * @return 结果
     */
    @Override
    public List<EquipmentThreshold> queryThresholdList() {
        return equipmentThresholdMapper.selectList();
    }
}
